import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import '../../pages/view/PopRoute.dart';
import '../../pages/view/itemOrderView.dart';
import '../../services/TLSizeFit.dart';

import '../../YColors.dart';
import 'Popup.dart';

/**
 *  审核页面
 *
 */
class VerifyPage extends StatefulWidget {
  VerifyPage({Key key}) : super(key: key);

  _VerifyState createState() => _VerifyState();
}

class _VerifyState extends State<VerifyPage> {
  EasyRefreshController _controller;

  // 条目总数
  int _count = 1; //20

  String selectStr = "";
  List listStr = new List();
  List listImage = new List();
  List listIndex = new List();
  double screenWith;

  _VerifyState() {
//    for (var i = 0; i < 6; i++) {
//      this.list.add('我是第$i');
//    }
    this.listStr.add('我的待办');
    this.listStr.add('待我处理');
    this.listStr.add('我发起的');
    this.listStr.add('我参与的');
    this.listStr.add('已处理的');
    this.listStr.add('审批事项');
    this.listStr.add('项目任务');

    selectStr = listStr[0];

    listImage.add('images/personcenter1.png');
    listImage.add('images/cancel_order.png');
    listImage.add('images/myorder1.png');
    listImage.add('images/marketdemand_on.png');
    listImage.add('images/personcenter1.png');
    listImage.add('images/cancel_order.png');
    listImage.add('images/myorder1.png');
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    _controller = EasyRefreshController();
    TLSizeFit.initialize();
    screenWith = TLSizeFit.getScreenWidth();

    print("screenWith:$screenWith"); //1080
    print("screenWith:${TLSizeFit.physicalWidth}"); //1080
    print("TLSizeFit.screenHeight:${TLSizeFit.screenHeight}");      // 758.66
    print("TLSizeFit.physicalHeight:${TLSizeFit.physicalHeight}");  // 2276
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        // 标题居中
        elevation: 0,
        //隐藏底部阴影分割线
        title: InkWell(
          child: Text(
            "我是home页新",
            style: TextStyle(color: YColors.r, fontSize: 18),
          ),
          onTap: () {
            print("....点击了弹框");
            _clickPopWindow();
          },
        ),
//            Text(
//          "审核页面",
//          style: TextStyle(color: YColors.r, fontSize: 18),
//        ),
        backgroundColor: Colors.white,
      ),
      body: Container(
        color: YColors.f,
//        height: double.infinity,
//        padding:
//        EdgeInsets.fromLTRB(TLSizeFit.setPx(0), 0, TLSizeFit.setPx(0), 0),
        child: setBodyView(),
      ),
    );
  }

  Widget setBodyView() {
    return Column(
      children: <Widget>[
//        Container(
//          alignment: Alignment.center,
//          child: InkWell(
//            child: Text("我是审核页"),
//            onTap: () {
//              print("....点击了弹框");
//              _clickPopWindow();
//            },
//          ),
//        ),
        Container(
          color: YColors.f,
//         margin: EdgeInsets.fromLTRB(0, 0, 0, 280),
          height: TLSizeFit.screenHeight - TLSizeFit.setPx(110),
          child: EasyRefresh.custom(
            shrinkWrap: true,
            enableControlFinishRefresh: false,
            // 是否开启控制结束刷新
            enableControlFinishLoad: true,
            // 是否开启控制结束加载
            controller: _controller,
            // EasyRefresh控制器
//          header: ClassicalHeader(),
            header: ClassicalHeader(
              enableInfiniteRefresh: false,
              refreshText: "拉动刷新",
              refreshReadyText: "释放刷新",
              refreshingText: "正在刷新...",
              refreshedText: "刷新完成",
              refreshFailedText: "刷新失败",
              noMoreText: "没有更多数据",
              showInfo: false, // 是否显示时间
//            infoText: "更新于 %T",
            ),
//          footer: ClassicalFooter(),
            footer: ClassicalFooter(
              loadText: "拉动加载",
              loadReadyText: "释放加载",
              loadingText: "正在加载...",
              loadedText: "加载完成",
              loadFailedText: "加载失败",
              noMoreText: "没有更多数据",
              infoText: "更新于 %T",
            ),
            onRefresh: () async {
              await Future.delayed(Duration(seconds: 2), () {
                print('onRefresh');
                setState(() {
                  _count = 5;
                });
                _controller.resetLoadState();
              });
            },
            onLoad: () async {
              await Future.delayed(Duration(seconds: 2), () {
                print('onLoad');
                setState(() {
                  _count += 1;
                });
                _controller.finishLoad(noMore: _count >= 40);
              });
            },
            slivers: <Widget>[
              SliverList(
                delegate: SliverChildBuilderDelegate(
                  (context, index) {
                    return ItemOrderView();
//                  return Container(
//                    width: 60.0,
//                    height: 60.0,
//                    child: Center(
//                      child: Text('$index'),
//                    ),
//                    color:
//                        index % 2 == 0 ? Colors.grey[300] : Colors.transparent,
//                  );
                  },
                  childCount: _count,
                ),
              ),
            ],
          ),
        )
      ],
    );
  }

  ///点击弹框调用onClick
  void _clickPopWindow() {
    Navigator.push(
      context,
      PopRoute(
        child: Popup(
          child: _buildPopShow(),
//          left: TLSizeFit.setPx(100),
          top: TLSizeFit.setPx(84),  // 108
//          onClick: () {
//            Navigator.of(context).pop();
//            print("exit");
//          },
        ),
      ),
    );
  }

//  原文链接：https://blog.csdn.net/email_jade/article/details/87922051

  //弹出框的布局
  Widget _buildPopShow() {
    return Stack(
      children: <Widget>[
        Opacity(
          opacity: 0.6, //设置透明度
          child: Container(
            color: YColors.d,
            width: screenWith,
//              height: TLSizeFit.setPx(620),
            height: TLSizeFit.screenHeight,
            padding: EdgeInsets.all(16.0),
            alignment: Alignment.bottomCenter,
          ),
        ),
        Container(
//      margin: EdgeInsets.fromLTRB(0, 10, 0, 0),
          width: screenWith,
//          height: TLSizeFit.getScreenHeight(),
//      height: TLSizeFit.setPx(500),
//          color: YColors.d,

          child: Stack(
            children: <Widget>[
              Container(
//            margin: EdgeInsets.fromLTRB(0, 0, 0, 0),
                width: TLSizeFit.setPx(375),
                height: TLSizeFit.setPx(360),
                child: Column(
                  children: <Widget>[
                    setPopItemView(this.listStr[0], this.listImage[0], "4"),
                    setPopItemView(this.listStr[1], this.listImage[1], ""),
                    setPopItemView(this.listStr[2], this.listImage[2], ""),
                    setPopItemView(this.listStr[3], this.listImage[3], "1"),
                    setPopItemView(this.listStr[4], this.listImage[4], "2"),
                    setPopItemView(this.listStr[5], this.listImage[5], ""),
                    setPopItemView(this.listStr[6], this.listImage[6], ""),
//                    Expanded(
//                      child: ListView.builder(
//                        itemCount: this.listStr.length,
//                        itemBuilder: (context, index) {
//                          return Container(
////                    margin: EdgeInsets.fromLTRB(20, 0, 40, 0),
////                        color: YColors.l,
//                            height: TLSizeFit.setPx(40),
////                    width: TLSizeFit.setPx(200),
////                    child: Text(this.listStr[index]),
//                            child: setPopItemView(this.listStr[index],
//                                this.listImage[index], "2"),
//                          );
//                        },
//                      ),
//                    ),
                  ],
                ),

//            color: YColors.l,  // 不能有，和decoration 冲突
//            child: Text(
//              "卡金风科技",
//            ),
//            child: setPopItemView(this.listStr[1], this.listImage[1], "2"),
//            child: setPopItemView("待审批", "images/personcenter1.png", "2"),
//                child: Expanded(
//                  child: ListView.builder(
//                    itemCount: this.listStr.length,
//                    itemBuilder: (context, index) {
//                      return Container(
////                    margin: EdgeInsets.fromLTRB(20, 0, 40, 0),
////                        color: YColors.l,
//                        height: TLSizeFit.setPx(40),
////                    width: TLSizeFit.setPx(200),
////                    child: Text(this.listStr[index]),
//                        child: setPopItemView(
//                            this.listStr[index], this.listImage[index], "2"),
//                      );
//                    },
//                  ),
//                ),
                decoration: BoxDecoration(
                  color: YColors.t,
                  borderRadius: BorderRadius.only(
                    bottomLeft: Radius.circular(10),
                    bottomRight: Radius.circular(10),
                  ),
                ),
              ),
            ],
          ),
        ),
      ],
    );
  }

  /**
   * 使用写死的单个条目，而没有用listview,
   *  listview ,会距离顶部，
   */
  Widget setPopItemView(textStr, imageStr, number) {
    return InkWell(
      child: Container(
//        color: selectStr == textStr ?  YColors.l : YColors.f,
//        color: YColors.l,
        height: TLSizeFit.setPx(40),
        margin: EdgeInsets.fromLTRB(TLSizeFit.setPx(10), TLSizeFit.setPx(2),
            TLSizeFit.setPx(10), TLSizeFit.setPx(6)),
        padding:
            EdgeInsets.fromLTRB(TLSizeFit.setPx(10), 0, TLSizeFit.setPx(10), 0),
        child: Row(
          children: <Widget>[
            Image.asset(
              imageStr,
              width: TLSizeFit.setPx(28),
              height: TLSizeFit.setPx(28),
            ),
            SizedBox(width: 14),
            Expanded(
                child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: <Widget>[
                Text(
                  textStr,
                  style: TextStyle(fontSize: 14, color: YColors.a),
                ),
                Text(
                  number,
                  style: TextStyle(fontSize: 14, color: YColors.a),
                ),
              ],
            )),
          ],
        ),

        decoration: selectStr == textStr
            ? BoxDecoration(
                color: YColors.f,
                borderRadius: BorderRadius.all(// 圆角
                    //  Radius.circular(150),    //圆形
                    Radius.circular(6) // 圆角 zj
                    ),
                // 背景色 zj
              )
            : BoxDecoration(),
      ),
      onTap: () {
        print('点击了：$textStr');
        setState(() {
          selectStr = textStr;
        });
        Navigator.of(context).pop();
      },
    );
  }
}
